package com.david.func_test.view

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.david.func_test.R

/**
 * 用于添加labels的view，可换行
 * TODO 1.标签后面添加追加文本, 2.单独设置item字体颜色、边框颜色、背景色
 */
class LabelsView:View {
    private var spaceHorizontal = 30f//item之间的间距
    private var spaceVertical = 10f//item之间的间距

    private var paddingHorizontal = 10
    private var paddingVertical = 5

    private var radius = 10f//标签的圆角

    private val mPaint: Paint = Paint()

    private var textSize = 26f
    private var textColor = Color.YELLOW

    private var defaultBgColor = Color.GRAY
    private var defaultTextColor = Color.WHITE

    private val labels = ArrayList<LabelBean>()

    private var textStrokeWidth = 0f//默认的文字笔画宽度，有strokeWidth时paint的strokeWidth值会被修改，此值用于恢复文字strokeWidth

    constructor(context: Context):super(context)
    constructor(context: Context, attr: AttributeSet):super(context, attr) {
        val typedArray: TypedArray =
            context.obtainStyledAttributes(attr, R.styleable.LabelsView)

        spaceHorizontal = typedArray.getDimension(R.styleable.LabelsView_space_horizontal, 10f)
        spaceVertical = typedArray.getDimension(R.styleable.LabelsView_space_vertical, 10f)
        paddingHorizontal = typedArray.getDimension(R.styleable.LabelsView_padding_horizontal, 10f).toInt()
        paddingVertical = typedArray.getDimension(R.styleable.LabelsView_padding_vertical, 10f).toInt()

        radius = typedArray.getDimension(R.styleable.LabelsView_radius, 10f)
        textSize = typedArray.getDimension(R.styleable.LabelsView_text_size, 26f)
        textColor = typedArray.getColor(R.styleable.LabelsView_text_color, Color.BLACK)

        defaultBgColor = typedArray.getInt(R.styleable.LabelsView_bg_color, Color.GRAY)
        defaultTextColor = typedArray.getInt(R.styleable.LabelsView_text_color, Color.WHITE)

        mPaint.textSize = textSize

        typedArray.recycle()

        textStrokeWidth = mPaint.strokeWidth
    }

    fun addLabels(labels: List<String>){
        val labelBeans = ArrayList<LabelBean>()
        for (item in labels) {
            labelBeans.add(LabelBean(item))
        }

        addLabels(labelBeans)
    }

    fun addLabels(labels:ArrayList<out LabelBean>){
        this.labels.addAll(labels)
        requestLayout()
    }

    fun resetLabels(labels: List<String>){
        val labelBeans = ArrayList<LabelBean>()
        for (item in labels) {
            labelBeans.add(LabelBean(item))
        }

        resetLabels(labelBeans)
    }

    fun resetLabels(labels:ArrayList<out LabelBean>){
        this.labels.clear()
        this.labels.addAll(labels)
        requestLayout()
    }

    private var startX = 0f//计算下个item的左边位置
    private var startY = 0f

    private var textWidth = 0f

    //测量每个item放置位置
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        startX = paddingLeft.toFloat()
        startY = paddingTop.toFloat()

        for (item in labels) {
            textWidth = mPaint.measureText(item.text)

            item.labelLeft = startX
            item.labelTop = startY
            if ((startX + textWidth + (paddingHorizontal shl 1) + (paddingLeft + paddingRight)) > measuredWidth) {//item到达右侧，换行
                startX = paddingLeft.toFloat()
                startY += (paddingVertical shl 1) + mPaint.fontMetrics.bottom - mPaint.fontMetrics.top+ spaceVertical

                item.labelLeft = startX
                item.labelTop = startY
            }
            item.labelRight = startX + textWidth + (paddingHorizontal shl 1)
            item.labelBottom = startY + mPaint.fontMetrics.bottom - mPaint.fontMetrics.ascent + (paddingVertical shl 1)

            startX += spaceHorizontal + (paddingHorizontal shl 1)
            startX += textWidth
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        for (item in labels) {
            item.text?.let {

                //绘制背景
                if (item.bgColor != 0) {
                    mPaint.color = item.bgColor
                    canvas.drawRoundRect(item.labelLeft, item.labelTop, item.labelRight,
                        item.labelBottom, radius, radius, mPaint)
                }

                //绘制边框
                if (item.strokeColor != 0) {
                    mPaint.style = Paint.Style.STROKE
                    mPaint.strokeWidth = item.strokeWidth
                    mPaint.color = item.strokeColor
                    canvas.drawRoundRect(item.labelLeft, item.labelTop, item.labelRight,
                        item.labelBottom, radius, radius, mPaint)
                }

                mPaint.style = Paint.Style.FILL
                //绘制文字
                if (item.textColor > 0) {
                    mPaint.color = item.textColor
                } else {
                    mPaint.color = defaultTextColor
                }
                mPaint.strokeWidth = textStrokeWidth
                mPaint.color = textColor
                canvas.drawText(it, item.labelLeft + paddingHorizontal, item.labelTop + paddingVertical + (mPaint.fontMetrics.leading - mPaint.fontMetrics.ascent)//绘制文字顶在view顶部
                    , mPaint)
            }
        }
    }

//    private var touchX = 0f
//    private var touchY = 0f
//    override fun onTouchEvent(event: MotionEvent?): Boolean {
//        when(event?.action) {
//            MotionEvent.ACTION_DOWN -> {
//                touchX = event.getX(0)
//                touchY = event.getY(0)
//                for (item in labels) {
//                    if (item.labelLeft < touchX && touchX < item.labelRight
//                        && item.labelTop < touchY && touchY < item.labelBottom
//                    ) {//点击在了标签里
//                        //可以修改item的背景色添加点击效果
//                        return true
//                    }
//                }
//                return false
//            }
//            MotionEvent.ACTION_UP -> {
//                touchX = event.getX(0)
//                touchY = event.getY(0)
//                for (item in labels) {
//                    if (item.labelLeft < touchX && touchX < item.labelRight
//                        && item.labelTop < touchY && touchY < item.labelBottom
//                    ) {//点击在了标签里，相应事件
//                        // TODO: 相应点击事件
//                        return true
//                    }
//                }
//            }
//        }
//        return super.onTouchEvent(event)
//    }
}

open class LabelBean (var text: String? = null)   {
    var labelLeft = 0f//标签的上下左右位置，用于在对应位置绘制item
    var labelTop = 0f
    var labelRight = 0f
    var labelBottom = 0f

    var bgColor: Int = 0//用于设置单个item背景
    var strokeColor: Int = 0//用于设置单个item边框颜色
    var strokeWidth: Float = 0f//用于设置单个item边框宽度
    var textColor: Int = 0//用于设置单个item文字颜色
}